package com.example.front.main.user;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.front.MyApplication;
import com.example.front.R;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

public class PhoneModify extends AppCompatActivity {
    private MyApplication myApplication;
    private EditText editText;
    private String url;

    @Override
    protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        setContentView(R.layout.phone_modify);

        myApplication = (MyApplication) getApplication();
        editText = (EditText) findViewById(R.id.phone);
        url = myApplication.getURL();
    }

    public void phone_onclick(View view){

        String phone = editText.getText().toString();
        if (phone.equals("")){
            Toast.makeText(PhoneModify.this,"请输入手机号",Toast.LENGTH_SHORT).show();
        }else {
            SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
            String account = sharedPreferences.getString("account", "");
            OkHttpUtils.post()
                    .url(url + "/user/mphone")
                    .addParams("account", account)
                    .addParams("phone", phone)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            Toast.makeText(PhoneModify.this, "网络错误，修改失败", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onResponse(String response) {
                            if (response.equals("true")) {
                                SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString("phone", phone);
                                editor.apply();
                                Toast.makeText(PhoneModify.this, "修改成功", Toast.LENGTH_SHORT).show();
                                finish();
                            } else {
                                Toast.makeText(PhoneModify.this, "系统错误，修改失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }
    }
}
